/*
 * 	S-EXPRESSIONS - Efficient representation of structured data.
 * 
 *  Copyright 2011 Jason Feingold
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at;
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.;
 */package sexpr

import org.scalatest.FunSuite

class TestReader  extends FunSuite
{
	def check (s : SExpr.ReaderString, c : Char, p : SExpr.Pos, t : Char) { expect (c) (t) ; expect (p) (s.pos) }
	test ("[blank]")
	{
		val s = new SExpr.ReaderString ("")
		expect ((1,1)) (s.pos)
		intercept [Throwable] (s.peek)
		intercept [Throwable] (s.take)
		intercept [Throwable] (s.eatWhite)
	}
	test ("a")
	{
		val s = new SExpr.ReaderString ("a")
		check (s, 'a', (1,1), s.peek)
		s.eatWhite
		check (s, 'a', (1,2), s.take)
		intercept [Throwable] (s.peek)
		intercept [Throwable] (s.take)
		intercept [Throwable] (s.eatWhite)
	}
	test ("abcde")
	{
		val s = new SExpr.ReaderString ("abcde")
		expect ((1,1)) (s.pos)
		check (s, 'a', (1,1), s.peek)
		s.eatWhite
		check (s, 'a', (1,2), s.take)
		s.eatWhite
		check (s, 'b', (1,2), s.peek)
		s.eatWhite
		check (s, 'b', (1,3), s.take)
		s.eatWhite
		check (s, 'c', (1,3), s.peek)
		s.eatWhite
		check (s, 'c', (1,3), s.peek)
		s.eatWhite
		check (s, 'c', (1,3), s.peek)
		s.eatWhite
		check (s, 'c', (1,3), s.peek)
		s.eatWhite
		check (s, 'c', (1,4), s.take)
		s.eatWhite
		s.take
		s.take
		intercept [Throwable] (s.peek)
		intercept [Throwable] (s.take)
		intercept [Throwable] (s.eatWhite)
	}
	test ("whitespace")
	{
		val s = new SExpr.ReaderString ("  a  e")
		expect ((1,1)) (s.pos)
		s.eatWhite
		s.eatWhite
		expect ((1,3)) (s.pos)
		check (s, 'a', (1,3), s.peek)
		check (s, 'a', (1,4), s.take)
		s.eatWhite
		s.eatWhite
		check (s, 'e', (1,6), s.peek)
		check (s, 'e', (1,6), s.peek)
		check (s, 'e', (1,6), s.peek)
		check (s, 'e', (1,7), s.take)
		intercept [Throwable] (s.peek)
		intercept [Throwable] (s.take)
		intercept [Throwable] (s.eatWhite)
	}
	test ("new lines")
	{
		val s = new SExpr.ReaderString (" \n a \n e")
		expect ((1,1)) (s.pos)
		s.eatWhite
		s.eatWhite
		expect ((2,2)) (s.pos)
		check (s, 'a', (2,2), s.peek)
		check (s, 'a', (2,3), s.take)
		s.eatWhite
		s.eatWhite
		check (s, 'e', (3,2), s.peek)
		check (s, 'e', (3,2), s.peek)
		check (s, 'e', (3,2), s.peek)
		check (s, 'e', (3,3), s.take)
		intercept [Throwable] (s.peek)
		intercept [Throwable] (s.take)
		intercept [Throwable] (s.eatWhite)
	}
}